#!/usr/bin/env bash

# TODO: add more functionalities

help(){
    cat<<__EOF__
    usage: m finder [ showhiddenfiles | showfileextensions | showdesktop | help ]

    Examples:
      m finder showhiddenfiles           # get the current status
      m finder showhiddenfiles YES       # show hidden files
      m finder showhiddenfiles NO        # don't show hidden files

      m finder showextensions            # get the current status
      m finder showextensions YES        # show all file extensions
      m finder showextensions NO         # don't show all file extensions

      m finder showdesktop               # get the current desktop status
      m finder showdesktop YES           # enable the desktop
      m finder showdesktop NO            # disable the desktop
__EOF__
}

hidden_files(){
    case $1 in
        [yY][eE][sS])
            echo "Show hidden files: YES"
            defaults write com.apple.finder AppleShowAllFiles -bool true
            ;;
        [nN][oO])
            echo "Show hidden files: NO"
            defaults write com.apple.finder AppleShowAllFiles -bool false
            ;;
        *)
            HIDDEN_FILE_STATUS=$(defaults read com.apple.finder AppleShowAllFiles 2>/dev/null)

            case $HIDDEN_FILE_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    HIDDEN_FILE_STATUS="NO"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    HIDDEN_FILE_STATUS="YES"
                    ;;
                *)
                    echo "We can't read AppleShowAllFiles property" && exit 1
                    ;;
            esac

            echo "Show hidden files: $HIDDEN_FILE_STATUS"
            exit 0
            ;;
    esac
    killall Finder
}

file_extensions(){
    case $1 in
        [yY][eE][sS])
            echo "Show file extensions: YES"
            defaults write NSGlobalDomain AppleShowAllExtensions -bool true
            ;;
        [nN][oO])
            echo "Show file extensions: NO"
            defaults write NSGlobalDomain AppleShowAllExtensions -bool false
            ;;
        *)
            EXTENSION_STATUS=$(defaults read NSGlobalDomain AppleShowAllExtensions 2>/dev/null)
            case $EXTENSION_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    EXTENSION_STATUS="NO"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    EXTENSION_STATUS="YES"
                    ;;
                *)
                    echo "We can't read AppleShowAllExtension property" && exit 1
                    ;;
            esac
            echo "Show file extensions: $EXTENSION_STATUS"
            exit 0
            ;;
    esac
    killall Finder
}

desktop(){
    case $1 in
        [yY][eE][sS])
            echo "Enable desktop: YES"
            defaults write com.apple.finder CreateDesktop -bool true
            ;;
        [nN][oO])
            echo "Enable desktop: NO"
            defaults write com.apple.finder CreateDesktop -bool false
            ;;
        *)
            DESKTOP_STATUS=$(defaults read com.apple.finder CreateDesktop 2>/dev/null)
            case $DESKTOP_STATUS in
                0|[nN][oO]|[fF][aA][lL][sS][eE])
                    DESKTOP_STATUS="disabled"
                    ;;
                1|[yY][eE][sS]|[tT][rU][eE])
                    DESKTOP_STATUS="enabled"
                    ;;
                *)
                    echo "We can't read CreateDesktop property" && exit 1
                    ;;
            esac
            echo "Desktop: $DESKTOP_STATUS"
            ;;
    esac
    killall Finder
}

case $1 in
    help)
        help
        ;;
    showhiddenfiles)
        shift
        hidden_files "$@"
        ;;
    showextensions)
        shift
        file_extensions "$@"
        ;;
    showdesktop)
        shift
        desktop "$@"
        ;;
    *)
        help
        ;;
esac

# vim: ts=4 sw=4 softtabstop=4 expandtab
